Table of Contents
EventEmitter
A generator for EventEmitter, lets you create an objects that can emit events, or generate a new generator that inherits from EventEmitter.
Install:
$ npm install generate-js-events
EventEmitter.create()
- return:
Object
A new object that inherits from EventEmitter.
Creates a new object that inherits from EventEmitter.
Example:
var EventEmitter = require('generate-js-events');
var myEmitter = EventEmitter.create();
EventEmitter.generate(create)
- create
Function
Create method that gets called when creating a new object that inherits from EventEmitter. - return:
Generator
A new generator that inherits from EventEmitter.
Returns a new generator that inherits from EventEmitter.
Example:
var myGenerator = EventEmitter.generate(
function myGenerator() {
}
);
myGenerator.definePrototype(
{
},
{
}
);
Class: Generation
A new generator that inherits from the generator that generated it using the EventEmitter.generate(create) method.
Class: Creation
A new object that inherits from the generator that created it using the EventEmitter.create() method.
Creation.on(event, listener[, observer]
- event
String
Name of event. - listener
Function
Event handler function. - observer
Object
Object reference for binding. - return:
Object
This object.
Adds a 'listener' on 'event' to this EventEmitter instance.
Example:
myEmitter.onerror = function (err) {
console.log(err);
}
myEmitter.on('myevent', function(a, b, c) {
console.log(a, b, c);
});
Creation.once(event, listener[, observer])
-
event String
Name of event.
-
listener Function
Event handler function.
-
observer Object
Object reference for binding.
-
return: Object
This object.
Adds a 'listener' on 'event' to this EventEmitter instance which is removed after one 'event'.
Example:
myEmitter.once('myobjectevent', function(event) {
console.log(event);
});
Creation.off([event][, listener][, observer])
- event
String
Name of event. - listener
Function
Event handler function. - observer
Object
Object reference for binding. - return:
Object
This object.
Removes a 'listener' on 'event', or all listeners on 'event', or all listeners from this EventEmitter instance.
Example:
function specific(event) {
console.log(event);
}
myEmitter.on('specific', specific);
myEmitter.off('specific', specific);
myEmitter.off('same');
myEmitter.off();
Creation.emit(event, [...args])
- event
String
Name of event. - args
Arguments
All other arguments
to be emitted to listeners. - return:
Object
This object.
Emits an 'event' with 'args' on this EventEmitter instance.
Example:
myEmitter.emit('myevent', 1, 2, 3);
Creation.emitEvent(event, eventObject)
- event
String
Name of event. - eventObject
Object
An event object to be emitted to listeners. - return:
Object
This object.
Emits an event object containing 'eventObject' on this EventEmitter instance.
Example:
myEmitter.emitEvent('myobjectevent', {eventData: 'data'});